package org.winexpress

import grails.converters.JSON
import org.codehaus.groovy.grails.web.json.JSONObject
import org.codehaus.groovy.grails.web.json.JSONArray

class ProductTypeController {
    // shared methods
    def sendResponse(statusCode) {
        if (statusCode == 404) {
            JSONObject o = new JSONObject()
            o.put(statusCode, true)
            render o as JSON
        }
        
        response.status = statusCode
        render response
        
        // 200-OK (e.g. GET success)
        // 201-Created (e.g. POST success)
        // 403-Forbidden (e.g. LOGIN failure)
        // 400-Bad Request (e.g. PUT,POST,DELETE failure)
        // 404-Not Found (e.g. GET failure)
        // 500-Internal Server Error (other...)
    }
    
    

    // REST operations
    // GET
    def show = {
//        println 'ProductTypeController >> Show >> START'
        
        ProductTypeManagement ptman = new ProductTypeManagement()
        
        if (params.id && ProductType.exists(params.id)) {    
            def pt = ptman.getProductTypeByID(params.id)
            render pt as JSON
            sendResponse(200)
            
//            println 'Show Product type by ID >> ' + pt.id + " " + pt
        }
        else {
            def allPT = ptman.listProductTypes()
            if (allPT.length() > 0) {
                render allPT as JSON
                sendResponse(200)
            }
            else {
                sendResponse(404)
            }  
            
//            println 'Listing all Product type >> ' + allPT
        }
        
//        println 'ProductTypeController >> Show >> END'
    }
    
    // PUT
    def update = {
        println 'ProductTypeController >> Update >> START'
        
//        JSONObject o
//        def pt
//        def id
//        def ProductType
//        def ProductDesc
        
        ProductTypeManagement ptman = new ProductTypeManagement()
        
        
//        if (params.ProductType && params.ProductDesc) {
//            println 'ProductTypeController >> Update >> NO JSON'
//            pt = ptman.getProductTypeByID(params.id)      
//            ProductType = params.ProductType
//            ProductDesc = params.ProductDesc
//            
////            pt.id = id
//            pt.ProductTypeName = ProductType
//            pt.description = ProductDesc
//        }
//        else {
//            println 'ProductTypeController >> Update >> JSON'
//            o = request.JSON
//            
//            pt = ptman.getProductTypeByID(o.get("id"))
//            if (o.has("ProductTypeName")) { pt.ProductTypeName = o.get("ProductTypeName"); println pt.ProductTypeName }
//            if (o.has("description")) { pt.description = o.get("description"); println pt.description }
//        }
//        
//        def result = ptman.updateProductType(pt)
//        if (result == true) {
//            println 'ProductTypeController >> Update >> ' + result
////            render response as JSON
//        }
//        else {
//            println 'ProductTypeController >> Update >> ' + result
//        }
           
        JSONObject o = request.JSON
            
        def pt = ptman.getProductTypeByID(o.get("id"))
        if (o.has("productTypeName")) { pt.ProductTypeName = o.get("productTypeName"); println pt.productTypeName }
        if (o.has("description")) { pt.description = o.get("description"); println pt.description }
        
        def result = ptman.updateProductType(pt)
        if (result == true) {
            println 'ProductTypeController >> Update >> ' + result
            sendResponse(200)
        }
        else {
            println 'ProductTypeController >> Update >> ' + result
            sendResponse(400)
        }
        

        println 'ProductTypeController >> Update >> END'

    }
    
    // DELETE
    def delete = {
        println 'ProductTypeController >> delete >> START'
        
        ProductTypeManagement ptman = new ProductTypeManagement()
        
        if (params.id && ProductType.exists(params.id)) {    
            def result = ptman.deleteProductType(id)
            
            if (result == true) {
                println 'ProductTypeController >> delete >> ' + result
                sendResponse(200)
            }
            else {
                println 'ProductTypeController >> delete >> ' + result
                sendResponse(400)
            }
        }
        
        println 'ProductTypeController >> delete >> END'
    }
    
    // POST
    def save = {
        println 'ProductTypeController >> Save >> START'
        
        JSONObject o = request.JSON
        println o
        
//        def productTypeName = params['productTypeName']
//        println productTypeName
        
        ProductTypeManagement ptman = new ProductTypeManagement()
        
        if (isUniqueProductTypeName(o.get("productTypeName")) == true) {
            def result = ptman.addProductType(o)
            if (result == true) {
                println 'ProductTypeController >> Save >> ' + result
                sendResponse(201)
            }
            else {
                println 'ProductTypeController >> Save >> ' + result
                sendResponse(400)
            }
        }
        else {
            sendResponse(406)
        }
        
        println 'ProductTypeController >> Save >> END'
        redirect(uri:"/ProductTypeCreate")
    }
    
    
    // supporting operations
    def isUniqueProductTypeName(productTypeName) {
        
        def status = false
        
        ProductTypeManagement ptman = new ProductTypeManagement()
        ProductType pt = null
        
        pt = ptman.getProductTypeForDbByProductTypeName(productTypeName)
        
        if (pt == null) {
            status = true 
        }
        
        return status
    }
}
